import {getLogger} from "log4js";
import { basic } from './fetch'


const logger = getLogger('basic/api/');

function normalized(url, type) {
  return url.startsWith('artemis') ? url : `/artemis-${type}/${url.split('/').pop()}`
}

function normalizedChat(url) {
  return normalized(url, 'chat')
}

function normalizedRecord(url) {
  return normalized(url, 'record')
}

/**
 * 查询所有的 通讯账号
 */
export function getAccount() {
  return basic.post('pubCommunicationAccount/list')
    .catch(logger.error)
    .then(res => res.json())
}

/**
 * 查询所有的 通讯组
 * {
        "id": "1070640291607875584",
        "orgId": "943520961360150528",
        "orgName": "南京电科院",
        "personId": "997153331191218176,995402451794305024,993475512590512128,955511639191347200,1057620922502193152,1057610041785577472,",
        "personName": "王林,创建者,焦刚,董家宁,赵佩,姜恒博,",
        "distributionGroupName": "拜拜菜单",
        "distributionGroupCode": "bb",
        "delFlag": 0,
        "createUserId": "1057690743654825984",
        "createTime": 1675302137000,
        "updateUserId": "1057628618974543872",
        "updateTime": 1675930509000,
        "personList": null,
        "status": null,
        "equipmentIds": "",
        "equipmentName": "",
        "contentPath": null,
        "accountList": null,
        "type": 0
    }
 */
export function getCommunicationGroup() {
  return basic.post('pubDistributionGroup/list')
    .catch(logger.error)
    .then(res => res.json())
}

/**
 * 更改用户【 状态 】
 */
export function toggleAccountState(account, status) {
  const body = {
    account,
    status
  }
  return basic.post('pubCommunicationAccount/resetState', { body })
    .catch(logger.error)
    .then(res => res.json())
}

/**
 * 记录【 登录 】
 */
export function loginRecord(
  {
    userId,
    username,
    upperTime,
    downTime,
  }
) {
  const body = {
    "communicationAccount": userId,
    "personName": username,

    "upperTime": upperTime + '',
    "downTime": downTime + '',
    "onlineTime": downTime - upperTime + '',
  }

  return basic.post('pubLogin/insert', { body })
    .catch(logger.error)
    .then(res => res.json())
}

/**
 * 聊天 - 消息记录
 *
 * @param userId
 * @param username
 * @param roomId
 * @param roomName
 * @param content
 * @param contentType { 0: 文字, 1: 路径 }
 */
export function chatMessageRecord(
  {
    userId,
    username,
    roomId,
    roomName,
    content,
    contentType,
  }
) {
  const body = {
    // "createUserId": userId,
    "communicationAccount": userId,
    // "communicationName": username,
    "userName": username,

    "roomId": roomId,
    "groupName": roomName,

    "contentPath": content,
    "contentType": contentType,

    "messageTime": Date.now().toString(),
    "startTime": Date.now().toString(),
  }

  console.log('发送聊天消息： =》 ', body)
  return basic.post('pubMessage/insert', { body })
    .catch(logger.error)
    .then(res => res.json())
}

/**
 * 通话记录
 *
 * @param classId
 * @param senderId
 * @param senderName
 * @param receives
 * @param chatMode
 * @param callTime
 * @param content
 */
export function callLogged(
  {
    classId,
    senderId,
    senderName,

    receives,

    chatMode,

    callTime,
    content,
  }
) {
  const body = {
    "groupId": classId,

    "callTime": callTime + '',
    "callDuration": Date.now() - callTime + '',

    "chatMode": chatMode,
    "contentPath": content,
    // 主叫
    "mainAccount": senderId,
    "mainName": senderName,
    // 被叫
    "calledAccount": receives.map(({ userId }) => userId).join(),
    "calledName": receives.map(({ username }) => username).join(),
  }

  if (Array.isArray(content)) {
    body.contentPath = content.map(normalizedRecord).join()
  } else {
    body.contentPath = normalizedRecord(content)
  }

  logger.trace('============== 保存通话记录 ================', body)
  return basic.post('pubCall/insert', { body })
    .catch(logger.error)
    .then(res => res.json())
}
